// Ionic Starter App

// angular.module is a global place for creating, registering and retrieving Angular modules
// 'starter' is the name of this angular module example (also set in a <body> attribute in index.html)
// the 2nd parameter is an array of 'requires'
// 'starter.services' is found in services.js
// 'starter.controllers' is found in controllers.js
angular.module('starter', ['ionic', 'starter.controllers', 'starter.services','starter.route','starter.directives','starter.constant','ngCordova'
,'starter.controllers_manage','video.controllers'])

.run(function($ionicPlatform,$cordovaNetwork,$rootScope,$location,$cordovaToast,$timeout, $ionicHistory,CONSTANT) {
  $ionicPlatform.ready(function() {
    // Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
    // for form inputs)
    if (window.cordova && window.cordova.plugins && window.cordova.plugins.Keyboard) {
      cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
      cordova.plugins.Keyboard.disableScroll(true);

    }
    if (window.StatusBar) {
      // org.apache.cordova.statusbar required
      StatusBar.styleDefault();
    }
    document.addEventListener("deviceready", function () {

      var type = $cordovaNetwork.getNetwork()

      var isOnline = $cordovaNetwork.isOnline()

      var isOffline = $cordovaNetwork.isOffline()

      var ws = null;
      // listen for Online event
      $rootScope.$on('$cordovaNetwork:online', function(event, networkState){
        var onlineState = networkState;
        $cordovaToast.show('有网络'+onlineState, 'long', 'bottom');
      })

      // listen for Offline event
      $rootScope.$on('$cordovaNetwork:offline', function(event, networkState){
        var offlineState = networkState;
        $cordovaToast.show('没有网络'+offlineState, 'long', 'bottom');
      })
    }, false);
  });
  //双击退出
  $ionicPlatform.registerBackButtonAction(function (e) {
    //判断处于哪个页面时双击退出
    if ($location.path() == '/tab/dash'||$location.path() == '/tab/chats'||$location.path() == '/tab/account') {
      if ($rootScope.backButtonPressedOnceToExit) {
        ionic.Platform.exitApp();
      } else {
        $rootScope.backButtonPressedOnceToExit = true;
        $cordovaToast.show('再按一次退出系统', 'short', 'bottom');
        setTimeout(function () {
          $rootScope.backButtonPressedOnceToExit = false;
        }, 2000);
      }
    }
    else if ($ionicHistory.backView()) {
      $ionicHistory.goBack();
    } else {
      $rootScope.backButtonPressedOnceToExit = true;
      $cordovaToast.show('再按一次退出系统', 'short', 'bottom');
      setTimeout(function () {
        $rootScope.backButtonPressedOnceToExit = false;
      }, 2000);
    }
    e.preventDefault();
    return false;
  }, 101);
})
.config(function($ionicConfigProvider) {
    $ionicConfigProvider.platform.ios.tabs.style('standard');
    $ionicConfigProvider.platform.ios.tabs.position('bottom');
    $ionicConfigProvider.platform.android.tabs.style('standard');
    $ionicConfigProvider.platform.android.tabs.position('bottom');
    $ionicConfigProvider.templates.maxPrefetch(0);
    $ionicConfigProvider.views.swipeBackEnabled(false);
    // $ionicConfigProvider.scrolling.jsScrolling(true);
  })
.config(['$httpProvider', function($httpProvider) {
    $httpProvider.interceptors.push('timeoutHttpIntercept');
  }]);

